#include "menubar.h"
#include <QAction>
#include <QMetaObject>
#include <QFile>
#include <QDebug>
#include <QFileDialog>
#include <QStandardPaths>
#include "../aurasim.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>

MenuBar::MenuBar(QWidget *parent) :
    QMenuBar(parent),
    mfileMenu(new QMenu(tr("File"), this)),
    mEditMenu(new QMenu(tr("Edit"), this)),
    mHelpMenu(new QMenu(tr("Help"), this))
{
    initUI();
    initConnections();
}

void MenuBar::initUI()
{
    this->addMenu(mfileMenu);
    this->addMenu(mEditMenu);
    this->addMenu(mHelpMenu);

    // file actions
    mFileActions << QStringPair(tr("open"), "actionOpen")
                 << QStringPair(tr("new project"), "actionNewProject")
                 << QStringPair(tr("save"), "actionSave")
                 << QStringPair(tr("save as"), "actionSaveas")
                 << QStringPair(tr("exit"), "actoinExit");
    foreach (const QStringPair& actionPair, mFileActions) {
        mfileMenu->addAction(actionPair.first);
    }

    // edit actions
    mEditActions << QStringPair(tr("undo"), "actionUndo")
                 << QStringPair(tr("redo"), "actionRedo");
    foreach (const QStringPair& actionPair, mEditActions) {
        mEditMenu->addAction(actionPair.first);
    }

    // help actions
    mHelpActions << QStringPair(tr("about"), "actionAbout")
                 << QStringPair(tr("document"), "actionDocument")
                 << QStringPair(tr("shortcuts"), "actionShortcuts");
    foreach (const QStringPair& actionPair, mHelpActions) {
        mHelpMenu->addAction(actionPair.first);
    }

    QFile qssFile(":/qss/menu-default.qss");
    if(qssFile.open(QIODevice::ReadOnly)) {
        QString qss = qssFile.readAll();
        this->setStyleSheet(qss);
        qssFile.close();
    }
}

void MenuBar::initConnections()
{
    connect(mfileMenu, &QMenu::triggered, [=](QAction* action){
        foreach (const QStringPair& actionPair, mFileActions) {
            if(actionPair.first == action->text()) {
                qDebug() << "trigging action: " << actionPair.second;
                if(this->metaObject()->indexOfMethod(QString("%1%2").arg(actionPair.second).arg("()").toStdString().data()) != -1) {
                    QMetaObject::invokeMethod(this, actionPair.second.toStdString().data());
                }
            }
        }
    });
}

void MenuBar::actionOpen()
{
}

void MenuBar::actionNewProject()
{

}

void MenuBar::actionSave()
{

}

void MenuBar::actionSaveas()
{
    QString filePath = QFileDialog::getSaveFileName(
                this,
                tr("Open Directory"),
                QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first(),
                tr("File (*.json)"));
    QJsonArray entits;
    foreach (Entity* e, Aurasim::entityManager()->entities()) {
        entits.append(Aurasim::toJsonObject(e));
    }
    QJsonDocument doc(entits);
    QString saveData = doc.toJson(QJsonDocument::Indented);
    QString savePath = filePath + ".json";
    QFile f(savePath);
    if(f.open(QIODevice::WriteOnly)) {
        f.write(saveData.toLatin1());
        f.close();
    }
}

void MenuBar::actionExit()
{

}

void MenuBar::actionUndo()
{

}

void MenuBar::actionRedo()
{

}

void MenuBar::actionAbout()
{

}

void MenuBar::actionDocument()
{

}

void MenuBar::actionShortcuts()
{

}
